<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClickTab">
        <el-tab-pane label="平台标签" name="first"></el-tab-pane>
        <el-tab-pane label="店铺标签" name="second"></el-tab-pane>

        <el-card class="filter-container" shadow="never">
          <div style="margin-top: 10px">
            <el-form :inline="true" size="small" label-width="140px">
              <el-form-item label="商家名称：" v-if="activeName === 'second'">
                <el-select
                  clearable
                  v-model="listQuery.store"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.storeId"
                    :label="item.storeName"
                    :value="item.storeId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="平台名称：" v-if="activeName === 'first'">
                <el-select
                  clearable
                  v-model="appQuery.platformId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="handleSecrch" type="primary" size="small"
                  >查询结果</el-button
                >
                <el-button @click="handleResetSearch" size="small">
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>

        <el-card class="operate-container" shadow="never">
          <el-button
            class="btn-add"
            icon="el-icon-plus"
            type="primary"
            @click="handleAdd()"
            size="mini"
          >
            添加
          </el-button>
        </el-card>

        <div style="margin-top: 10px">
          <el-table
            :data="list"
            border
            :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
          >
            <el-table-column
              v-if="activeName === 'first'"
              label="平台名称"
              align="center"
              prop="platformName"
            ></el-table-column>
            <el-table-column
              v-if="activeName === 'second'"
              label="店铺名称"
              align="center"
              prop="storeName"
            ></el-table-column>
            <el-table-column
              label="创建时间"
              align="center"
              prop="createTime"
            ></el-table-column>
            <el-table-column
              label="操作人"
              align="center"
              prop="editName"
            ></el-table-column>
            <el-table-column
              label="修改时间"
              align="center"
              prop="updateTime"
            ></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  @click="handleLabelProdut(scope.$index, scope.row)"
                  type="text"
                  >配置商品</el-button
                >
                <el-button
                  size="small"
                  @click="handleDetails(scope.$index, scope.row)"
                  type="text"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="listQuery.size"
            :page-sizes="[5, 10, 15]"
            :current-page.sync="listQuery.current"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tabs>

      <el-dialog title="添加商品" :visible.sync="dialogVisible" width="50%">
        <el-form :model="addForm" label-width="150px">
          <el-form-item label="商家名称：" v-if="activeName === 'second'">
            <el-select clearable v-model="addForm.storeId" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.storeId"
                :label="item.storeName"
                :value="item.storeId"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="平台名称：" v-if="activeName === 'first'">
            <el-select
              clearable
              v-model="addForm.platformId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit()">提交</el-button>
            <el-button type="info" @click="dialogVisible = true"
              >取消</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  platformSeleList,
  storeSeleList,
  seleDelete,
  hotSaleAdd,
} from "@/api/home/banner";
import { platformAll, getStore } from "@/api/rms/rmsstore";

export default {
  data() {
    return {
      activeName: "first",
      list: [],
      total: 0,
      listQuery: {
        current: 1,
        size: 10,
        store: null,
      },
      appQuery: {
        current: 1,
        size: 10,
        platformId: null,
      },
      options: [],
      dialogVisible: false,
      addForm: {
        storeId: null,
        platformId: null,
      },
    };
  },
  created() {
    this.getPlatformSeleList();
    this.getPlatformAll();
  },
  methods: {
    getPlatformAll() {
      platformAll().then((res) => {
        this.options = res.data.data;
      });
    },
    getStoreList() {
      getStore().then((res) => {
        this.options = res.data.data;
      });
    },
    getPlatformSeleList() {
      platformSeleList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    getStoreSeleList() {
      storeSeleList(this.appQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    handleClickTab(tab, event) {
      this.activeName = tab.name;
      if (this.activeName === "first") {
        this.appQuery.appId = null;
        this.getPlatformSeleList();
        this.getPlatformAll();
      } else if (this.activeName === "second") {
        this.listQuery.storeId = null;
        this.getStoreSeleList();
        this.getStoreList();
      }
    },
    handleSizeChange(val) {
      if (this.activeName === "first") {
        this.listQuery.size = val;
        this.getPlatformSeleList();
      } else if (this.activeName === "second") {
        this.appQuery.size = val;
        this.getStoreSeleList();
      }
    },
    handleCurrentChange(val) {
      if (this.activeName === "first") {
        this.listQuery.current = val;
        this.getPlatformSeleList();
      } else if (this.activeName === "second") {
        this.appQuery.current = val;
        this.getAppPage();
      }
    },

    handleSecrch() {
      if (this.activeName === "first") {
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.getAppPage();
      }
    },
    handleResetSearch() {
      if (this.activeName === "first") {
        this.listQuery.storeId = null;
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.appQuery.appId = null;
        this.getAppPage();
      }
    },
    handleAdd() {
      this.dialogVisible = true;
    },
    onSubmit() {
      if (this.activeName === "first") {
        this.platAdd();
      } else if (this.activeName === "second") {
        this.storeAdd();
      }
    },
    platAdd() {
      if (!this.addForm.platformId) {
        this.$message({
          message: "请选择平台~",
          type: "warning",
          duration: 1000,
        });
        return;
      }

      hotSaleAdd({ platformId: this.addForm.platformId }).then((res) => {
        if (res.data.code === 200) {
          this.dialogVisible = false;
          this.$message({
            message: res.data.msg,
            type: "success",
            duration: 1000,
          });
          this.getPlatformSeleList();
          this.getPlatformAll();
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning",
            duration: 1000,
          });
        }
      });
    },

    storeAdd() {
      if (!this.addForm.storeId) {
        this.$message({
          message: "请选择店铺~",
          type: "warning",
          duration: 1000,
        });
        return;
      }

      hotSaleAdd({ storeId: this.addForm.storeId }).then((res) => {
        if (res.data.code === 200) {
          this.dialogVisible = false;
          this.$message({
            message: res.data.msg,
            type: "success",
            duration: 1000,
          });
          this.getStoreSeleList();
          this.getStoreList();
        } else {
          this.$message({
            message: res.data.msg,
            type: "warning",
            duration: 1000,
          });
        }
      });
    },

    handleDetails(index, row) {
      let that = this;
      that
        .$confirm("是否确认删除ID为" + row.id, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(function () {
          seleDelete(row.id).then((res) => {
            if (res.data.code === 200) {
              that.$message({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              if (this.activeName === "first") {
                this.getPlatformSeleList();
                this.getPlatformAll();
              } else if (this.activeName === "second") {
                this.getStoreSeleList();
                this.getStoreList();
              }
            } else {
              that.$message({
                message: res.data.msg,
                type: "warning",
                duration: 1000,
              });
            }
          });
        });
    },
    handleLabelProdut(index, row) {
      if (this.activeName === "first") {
        this.$router.push({
          path: "/home/bestSele/add",
          query: {
            platformId: row.platformId,
            type: "platform",
          },
        });
      } else if (this.activeName === "second") {
        this.$router.push({
          path: "/home/bestSele/add",
          query: {
            storeId: row.storeId,
            type: "store",
          },
        });
      }
    },
  },
};
</script>